package com.wtwd.device.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.dao.AppDeviceBloodOxygenTotalMapper;
import com.wtwd.device.model.dto.DeviceBloodOxygenTotalDto;
import com.wtwd.device.model.entity.DeviceBloodOxygen;
import com.wtwd.device.model.entity.DeviceBloodOxygenTotal;
import com.wtwd.device.model.vo.BloodOxygenVo;
import com.wtwd.device.model.vo.DeviceBloodOxygenTotalVo;
import com.wtwd.device.service.AppDeviceBloodOxygenService;
import com.wtwd.device.service.AppDeviceBloodOxygenTotalService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AppDeviceBloodOxygenTotalServiceImpl extends ServiceImpl<AppDeviceBloodOxygenTotalMapper, DeviceBloodOxygenTotal> implements AppDeviceBloodOxygenTotalService {


    @Resource
    private AppDeviceBloodOxygenService appDeviceBloodOxygenService;

    @Resource
    private AppDeviceBloodOxygenTotalMapper appDeviceBloodOxygenTotalMapper;

    @Override
    public DeviceBloodOxygenTotalVo getDayBloodOxygenByTime(Long deviceId, Long id, String time) {
        DeviceBloodOxygenTotal deviceBloodOxygenTotal = getDayDeviceBloodOxygenTotalByTime(deviceId, time);
        if (deviceBloodOxygenTotal == null) {
            return null;
        }
        DeviceBloodOxygenTotalVo deviceBloodOxygenTotalVo = new DeviceBloodOxygenTotalVo();
        BeanUtils.copyProperties(deviceBloodOxygenTotal, deviceBloodOxygenTotalVo);
        List<DeviceBloodOxygen> deviceBloodOxygens = appDeviceBloodOxygenService.getBloodOxygenByTotalIdGtId(deviceBloodOxygenTotal.getId(), id);
        deviceBloodOxygenTotalVo.setBloodOxygens(deviceBloodOxygens);
        return deviceBloodOxygenTotalVo;
    }

    @Override
    public DeviceBloodOxygenTotal getDayDeviceBloodOxygenTotalByTime(Long deviceId, String time) {
        return new LambdaQueryChainWrapper<>(appDeviceBloodOxygenTotalMapper)
                .eq(DeviceBloodOxygenTotal::getDeviceId, deviceId)
                .eq(DeviceBloodOxygenTotal::getTime, time)
                .one();
    }

    @Override
    public List<DeviceBloodOxygenTotal> getDaysBloodOxyge(Long deviceId, String startTime, String endTime) {
        return new LambdaQueryChainWrapper<>(appDeviceBloodOxygenTotalMapper)
                .eq(DeviceBloodOxygenTotal::getDeviceId, deviceId)
                .between(DeviceBloodOxygenTotal::getTime, startTime, endTime)
                .list();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<DeviceBloodOxygenTotal> synchronizationBloodOxygen(List<DeviceBloodOxygenTotalDto> deviceBloodOxygenTotalDtos) {
        List<DeviceBloodOxygenTotal> deviceBloodOxygenTotals = new ArrayList<>();
        for (DeviceBloodOxygenTotalDto deviceBloodOxygenTotalDto : deviceBloodOxygenTotalDtos) {
            DeviceBloodOxygenTotal deviceBloodOxygenTotal = new DeviceBloodOxygenTotal();
            BeanUtils.copyProperties(deviceBloodOxygenTotalDto, deviceBloodOxygenTotal);
            DeviceBloodOxygenTotal deviceBloodOxygenTotalByTime = getDayDeviceBloodOxygenTotalByTime(deviceBloodOxygenTotalDto.getDeviceId(), deviceBloodOxygenTotalDto.getTime());
            if (deviceBloodOxygenTotalByTime == null) {
                save(deviceBloodOxygenTotal);
            }else {
                deviceBloodOxygenTotal.setId(deviceBloodOxygenTotalByTime.getId());
            }
            List<DeviceBloodOxygen> bloodOxygens = deviceBloodOxygenTotalDto.getBloodOxygens();
            if (bloodOxygens != null && bloodOxygens.size() > 0) {
                bloodOxygens.stream().forEach(e -> {
                    e.setDeviceBloodOxygenTotalId(deviceBloodOxygenTotal.getId());
                });
                appDeviceBloodOxygenService.saveBatch(bloodOxygens);
                DeviceBloodOxygen deviceBloodOxygen = appDeviceBloodOxygenService.getLastDeviceBloodOxygenByTotalId(deviceBloodOxygenTotal.getId());
                deviceBloodOxygenTotal.setLastInfoId(deviceBloodOxygen.getId());
                updateById(deviceBloodOxygenTotal);
            }
            deviceBloodOxygenTotals.add(deviceBloodOxygenTotal);
        }
        return deviceBloodOxygenTotals;
    }

    @Override
    public boolean insertOrUpdate(DeviceBloodOxygenTotal deviceBloodOxygenTotal) {
        if (deviceBloodOxygenTotal == null) return false;
        return appDeviceBloodOxygenTotalMapper.insertOrUpdate(deviceBloodOxygenTotal) > 0 ? true : false;
    }

    @Override
    public boolean bathInsertOrUpdate(List<DeviceBloodOxygenTotal> deviceBloodOxygenTotals) {
        if (deviceBloodOxygenTotals == null || deviceBloodOxygenTotals.size() == 0) {
            return false;
        }
        return appDeviceBloodOxygenTotalMapper.bathInsertOrUpdate(deviceBloodOxygenTotals) > 0 ? true : false;
    }

    @Override
    public BloodOxygenVo selectBloodOxygenByTime(Long deviceId, String time) {
        return appDeviceBloodOxygenTotalMapper.selectBloodOxygenByTime(deviceId,time);
    }


}
